package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageModel;
import study.saas.order.entity.Commodity;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.service.model.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CommodityService {
    @Resource
    private CommodityRepo commodityRepo;

    public ApiResultV2<Commodity> get(Integer commodityId) {
        Commodity commodity = commodityRepo.findFistByCommodityId(commodityId);
        if (commodity!= null && commodity.isDelete()) {
            commodity = null;
        }
        return ApiResultV2.ok(commodity);

    }

    public ApiResultV2 create(CommodityCreateDTO create) {
        Commodity commodity = commodityRepo.findFistByCommodityId(create.getCommodityId());
        if (commodity != null && !commodity.isDelete()) {
            return ApiResultV2.error("名称重复");
        }
        Commodity newCommodity;
        if (commodity == null) {
            newCommodity = new Commodity();
        } else {
            newCommodity = commodity;
            newCommodity.setIsDeleted(0);
        }
        CopyClassUtil.copyClass(create, newCommodity);
        commodityRepo.save(newCommodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 decrease(CommodityDecreaseDTO decrease) {
        Commodity commodity = commodityRepo.findFistByCommodityId(decrease.getCommodityId());
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.error("商品不存在");
        }
        CopyClassUtil.copyClass(decrease, commodity);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 delete(Integer commodityId) {
        Commodity commodity = commodityRepo.findFistByCommodityId(commodityId);
        if (commodity == null || commodity.isDelete()) {
            return ApiResultV2.ok();
        }
        commodity.setIsDeleted(1);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2<PageModel<Commodity>> list(CommodityFilterDTO filter) {
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select e from Commodity e where e.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getNameEqq())) {
            sql.append(" and e.name = :getNameEqq");
            param.put("getNameEqq", filter.getNameEqq());
        }
        if (!StringUtil.isEmpty(filter.getNameLikee())) {
            sql.append(" and e.name like :getNameLikee");
            param.put("getNameLikee", "%" + filter.getNameLikee() + "%");
        }
        sql.append(" order by e.createTime desc");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Commodity> pageModel = commodityRepo.queryPage(sql.toString(), param, Commodity.class, pageable);
        return ApiResultV2.ok(pageModel);
    }
}
